<template>
  <uni-popup ref="popupRef" background-color="#fff" :type="dialogType" :is-mask-click="false">
    <view>
      <template v-if="dialogDataOptions.type == 'message'">
        <uni-popup-message :duration="2000">
          <slot></slot>
        </uni-popup-message>
      </template>
      <template v-else-if="dialogDataOptions.type == 'share'">
        <uni-popup-share :title="dialogDataOptions.title" @select="dialogDataOptions.confirm" />
      </template>
      <template v-else>
        <uni-popup-dialog
          type="info"
          :mode="dialogDataOptions.type"
          :title="dialogDataOptions.title"
          :value="valueText"
          placeholder="请输入内容"
          :cancelText="dialogDataOptions.cancelText"
          :confirmText="dialogDataOptions.confirmText"
          :showClose="dialogDataOptions.showClose"
          @confirm="dialogDataOptions.confirm"
          @close="dialogDataOptions.close"
          :before-close="dialogDataOptions.beforeClose">
          <slot></slot>
        </uni-popup-dialog>
      </template>
    </view>
  </uni-popup>
</template>

<script setup lang="ts" name="IFormSelect">
import { computed, onMounted, PropType, ref, watch } from 'vue';
import { IDialogTemplate } from '#/container/IDialog';
const props = defineProps({
  dialogData: {
    type: Object as PropType<IDialogTemplate>,
    required: true
  }
});
const valueText = ref<string>(props.dialogData.value || '');
const dialogType = computed(() => (props.dialogData.type == 'input' ? 'dialog' : props.dialogData.type || 'dialog'));
const popupRef = ref();
const dialogDataOptions = computed(() => ({
  show: props.dialogData.show || false,
  type: props.dialogData.type || 'base',
  cancelText: props.dialogData.cancelText || '关闭',
  confirmText: props.dialogData.confirmText || '确定',
  title: props.dialogData.title,
  beforeClose: true,
  showClose: props.dialogData.close ? true : false,
  confirm: props.dialogData.confirm,
  close: props.dialogData.close
}));
watch(
  () => dialogDataOptions.value.show,
  New => {
    New ? popupRef.value.open() : popupRef.value.close();
  }
);
</script>
<style scoped lang="scss"></style>
